package cn.gaotu.basic.controller;


import cn.gaotu.basic.dto.SmsCodeDto;
import cn.gaotu.basic.excepion.BusinessException;
import cn.gaotu.basic.service.IVerifyCodeService;
import cn.gaotu.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 发送短信验证码
     * @param smsCodeDto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.smsCode(smsCodeDto);
            return new AjaxResult();
            //只要有业务异常都要加
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取手机验证码失败");
        }
    }

    @GetMapping("/image/{key}")
    public AjaxResult imageCode(@PathVariable("key") String key) {
        try {
            String code = verifyCodeService.imageCode(key);
            return new AjaxResult().setResultObj(code);
        } catch (BusinessException e) { //捕捉自己抛出的异常
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) { //500
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }

    }
}
